<?php
 $merchant='1000022215230183';
 $sn = getsn($merchant);
 $deviceInfo = 'deviceInfo1';
 $totalAmount='0.11';
 $subject = iconv("GB2312","UTF-8","苹果");
 $callBack = "http://******/qr/callback";
 $returnURL = "http://www.****.com/qr/return";
 $channel = "ali";
 $terminalType = "mobile";
 $tradeIP = "127.0.0.1";
 $remark = iconv("GB2312","UTF-8","通用付款");
 $key = 'UVZMMGDM';
 
 $content = $sn.$merchant.$deviceInfo.$totalAmount.$subject.$callBack.$returnURL.$channel.$terminalType.$tradeIP.$remark.$key;
 echo 'md5 sign content:'.$content.'</br>';
 $sign_md5 = md5($content);
 echo 'md5 sign value:'.$sign_md5.'</br>';
 
 $sign = sign_sha1($sign_md5);
 echo 'sha1 sign content:'.$sign.'</br>'; //sign value
 
 $keyStr16 = generate_randsn(16);
 echo 'keyStr16:'.$keyStr16.'</br>';
 
 $busiType='120001';
 $aes_content = $sign_md5.$busiType;
 echo 'aes_content:'.$aes_content.'</br>';
  
 $encryptData = sign_aes($aes_content,$keyStr16);
 echo 'encryptData:'.$encryptData.'</br>'; //encryptData value
 
 $encryptKey = sign_rsa($keyStr16);
 echo 'encryptKey:'.$encryptKey.'</br>'; //encryptKey value
 
 echo urlencode($sign).'</br>';;
 echo urlencode($encryptData).'</br>';
 echo urlencode($encryptKey).'</br>';;
 //post��ʽ
 $curlPost = 'merchant='.$merchant.'&sn='.$sn.'&deviceInfo='.$deviceInfo.'&totalAmount='.$totalAmount.'&subject='.$subject.'&callBack='.$callBack.'&returnURL='.$returnURL.'&channel='.$channel.'&terminalType='.$terminalType.'&tradeIP='.$tradeIP.'&busiType='.$busiType.'&remark='.$remark.'&sign='.urlencode($sign).'&encryptData='.urlencode($encryptData).'&encryptKey='.urlencode($encryptKey); 
 $ch=curl_init(); 
 curl_setopt($ch,CURLOPT_URL,'http://120.76.192.159:8170/BusiM/AL001/Check'); 
 curl_setopt($ch,CURLOPT_HEADER,0); 
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
 //������ͨ��post����get����
 curl_setopt($ch,CURLOPT_POST,1); 
 //���ݵı���
 curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost); 
 $data = curl_exec($ch); // ���󷵻�ֵ
 curl_close($ch);
 echo 'Req Data:'.$data.'</br>';
 
 $dataJson = json_decode($data);
 $codeRsp = $dataJson->codeRsp;
 echo 'codeRsp:'.json_encode($codeRsp).'</br>';
 $encrypt = $dataJson->encrypt;
 //encryptֵ����Ϊ��
 if ($encrypt){ 
	$en_encryptKey = $encrypt->encryptKey;
	$en_encryptData = $encrypt->encryptData;
	echo 'en_encryptKey:'.$en_encryptKey.'</br>';
	echo 'en_encryptData:'.$en_encryptData.'</br>';
	 
	$deKeyStr16 = decode_rsa(urldecode($en_encryptKey));
	echo '</br>'.'deKeyStr16:'.$deKeyStr16.'</br>';
 
	$deData = decode_aes($deKeyStr16,urldecode($en_encryptData));
	echo 'deData:'.$deData.'</br>';
	
	echo "--:".substr($deData,0,strrpos($deData,'}')+1);
	$dataJsonRs = json_decode(substr($deData,0,strrpos($deData,'}')+1));
	echo 'code:'.$dataJsonRs->code.'</br>';
 }
 
 function getsn($merchant){
	// return "Tsgu24utmkmzrx1fa45e08";
	return generate_randsn();
 }
 
 function sign_rsa($keyStr16){ 
    $signMsg;
    $pubilc_keys = openssl_get_publickey(file_get_contents('cer/system_rsa_public_key.pem'));
	openssl_public_encrypt($keyStr16, $signMsg, $pubilc_keys,OPENSSL_PKCS1_PADDING); //ע�����ɼ�����Ϣ
    return base64_encode($signMsg); //base64ת�������Ϣ
 }
 
  function decode_rsa($keySignStr){ 
    $signMsg;
    $private_keys = openssl_pkey_get_private(file_get_contents('cer/client_rsa_private_key.pem'));
	openssl_private_decrypt(base64_decode($keySignStr), $signMsg, $private_keys); //ע�����ɼ�����Ϣ
    return $signMsg; //base64ת�������Ϣ
 }

 function sign_aes($aes_content, $keyStr16){ 
	$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');      
	$iv_size = mcrypt_enc_get_iv_size($cipher);    
	$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
	if (mcrypt_generic_init($cipher, $keyStr16, $iv) != -1)    
	{ 
		$cipherText = mcrypt_generic($cipher,pad2Length($aes_content,16));    
		mcrypt_generic_deinit($cipher);    
		mcrypt_module_close($cipher);    
		   
		// Display the result in hex.    
		//printf("encrypted result:%s\n",base64_encode($cipherText));    
        return base64_encode($cipherText);		
	}  
 } 
 
  function decode_aes($keyStr16,$aes_content){ 
	$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');      
	$iv_size = mcrypt_enc_get_iv_size($cipher);    
	$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
	if (mcrypt_generic_init($cipher, $keyStr16, $iv) != -1)    
	{ 
		$cipherText = mdecrypt_generic($cipher,base64_decode($aes_content));    
		mcrypt_generic_deinit($cipher);    
		mcrypt_module_close($cipher);    
		   
		// Display the result in hex.    
		//printf("encrypted result:%s\n",base64_encode($cipherText));    
        return $cipherText;		
	}  
 } 
 
 function pad2Length($text, $padlen){    
    $len = strlen($text)%$padlen;    
    $res = $text;    
    $span = $padlen-$len;    
    for($i=0; $i<$span; $i++){    
        $res .= chr($span);    
    }    
    return $res;    
} 
 
 function sign_sha1($sign_md5){
	$signMsg;
	$private_keys = openssl_pkey_get_private(file_get_contents('cer/client_rsa_private_key.pem'));
	openssl_sign($sign_md5, $signMsg, $private_keys,OPENSSL_ALGO_SHA1); //ע�����ɼ�����Ϣ
    return base64_encode($signMsg); //base64ת�������Ϣ
 }

 function generate_randsn( $length = 28 ) { 
	// �����ַ������������������Ҫ���ַ� 
	$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	$sn = ''; 
	for ( $i = 0; $i < $length; $i++ ) 
	{ 
		// �����ṩ�����ַ���ȡ��ʽ 
		// ��һ����ʹ�� substr ��ȡ$chars�е�����һλ�ַ��� 
		// �ڶ�����ȡ�ַ����� $chars ������Ԫ�� 
		// $password .= substr($chars, mt_rand(0, strlen($chars) �C 1), 1); 
		$sn .= $chars[ mt_rand(0, strlen($chars) - 1) ]; 
	} 
	return $sn; 
} 
 ?>